package com.xindaoapp.happypet.viewpager.adapter;

import java.util.List;

import android.content.Context;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.adapter.XinDaoBaseAdapter;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.entry.MyReplyInfos;
import com.xindaoapp.happypet.entry.MyReplyInfos.ReplyInfo;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;

/**
 * 回复
 * 
 * @author xindaoapp
 */
public class ReplyAdapter extends XinDaoBaseAdapter<ReplyInfo> {

    private final int currentClickedPosition = -1;

    public ReplyAdapter(Context context, List<ReplyInfo> iniData, int pageSize, int res,
            int loadingRes) {
        super(context, iniData, pageSize, res, loadingRes);
    }

    @Override
    public void nextPage(int start, int size, final ILoadNextPageData<ReplyInfo> iLoadNextPageData) {
        HappyPetApplication
                .get()
                .getMoccaApi()
                .getMyReplyInfos(Constants.PAGE_FIRST, Constants.PAGE_SIZE,
                        new IRequest<MyReplyInfos>() {

                            @Override
                            public void request(MyReplyInfos t) {
                                iLoadNextPageData.loadNextPageData(t == null ? null : t.array);
                            }
                        });
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent,
            final ReplyInfo value) {
        ViewHolder holder = (ViewHolder) convertView.getTag();
        if (holder == null) {
            holder = new ViewHolder();
            holder.icon = (ImageView) convertView.findViewById(R.id.icon);
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.time = (TextView) convertView.findViewById(R.id.time);
            holder.content = (TextView) convertView.findViewById(R.id.content);
            holder.replyContent = (TextView) convertView.findViewById(R.id.reply_content);
            convertView.setTag(holder);
        }
        ImageLoader.getInstance().displayImage(value.userface, holder.icon);
        holder.name.setText(value.author);

        holder.time.setText(CommonUtil.formatTime2(value.addtime));
        // holder.content.setText(Html.fromHtml(value.subject));

        SpannableStringBuilder ss = (SpannableStringBuilder) Html.fromHtml(value.subject);
        SpannableStringBuilder temp = ss;
        if (currentClickedPosition != position && ss.length() > 20) {
            temp = (SpannableStringBuilder) ss.subSequence(0, 20);
            SpannableStringBuilder ssb =
                    CommonUtil.convetToHtml(CommonUtil.parseEmoji(temp.toString()), mContext);
            holder.content.setText(ssb + "...");
        } else {
            SpannableStringBuilder ssb =
                    CommonUtil.convetToHtml(CommonUtil.parseEmoji(temp.toString()), mContext);
            holder.content.setText(ssb);
        }

        holder.replyContent.setText(String.format("我：“%s”", Html.fromHtml(value.subject)));

        // convertView.setOnClickListener(new View.OnClickListener() {
        //
        // @Override
        // public void onClick(View v) {
        // Intent intent = new Intent(mContext, PostDetailActivity.class);
        // intent.putExtra(Constants.KEY_POST_TID, value.tid);
        // intent.putExtra("pageNum", Integer.valueOf(value.curpage));
        // intent.putExtra("floor", Integer.valueOf(value.curposition));
        // mContext.startActivity(intent);
        // }
        // });
        // convertView.setOnLongClickListener(new DeleteMessage(value.id));
        return convertView;
    }

    class ViewHolder {
        ImageView icon;
        TextView name;
        TextView time;
        TextView content;
        TextView replyContent;
    }
}
